package org.sbbs.base.util.excel;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.sbbs.app.demo.model.DemoEntity;

public class FillManager {
    /**
     * Fills the report with content
     *
     * @param worksheet
     * @param startRowIndex starting row offset
     * @param startColIndex starting column offset
     * @param datasource the data source
     */
    public static void fillReport( Sheet worksheet, int startRowIndex, int startColIndex, List<DemoEntity> datasource ) {
        // Row offset
        startRowIndex += 2;

        // Create cell style for the body
        CellStyle bodyCellStyle = worksheet.getWorkbook().createCellStyle();
        bodyCellStyle.setAlignment( CellStyle.ALIGN_CENTER );
        bodyCellStyle.setWrapText( true );
        /*
         * private Long demoId; private int intField; private long longField; private short shortField; private float
         * floatField; private double doubleField;
         */
        // Create body
        for ( int i = startRowIndex; i + startRowIndex - 2 < datasource.size() + 2; i++ ) {
            // Create a new row
            Row row = worksheet.createRow( (short) i + 1 );

            // Retrieve the id value
            Cell cell1 = row.createCell( startColIndex + 0 );
            cell1.setCellValue( datasource.get( i - 2 ).getDemoId() );
            cell1.setCellStyle( bodyCellStyle );

            // Retrieve the brand value
            Cell cell2 = row.createCell( startColIndex + 1 );
            cell2.setCellValue( datasource.get( i - 2 ).getIntField() );
            cell2.setCellStyle( bodyCellStyle );

            // Retrieve the model value
            Cell cell3 = row.createCell( startColIndex + 2 );
            cell3.setCellValue( datasource.get( i - 2 ).getLongField() );
            cell3.setCellStyle( bodyCellStyle );

            // Retrieve the maximum power value
            Cell cell4 = row.createCell( startColIndex + 3 );
            cell4.setCellValue( datasource.get( i - 2 ).getShortField() );
            cell4.setCellStyle( bodyCellStyle );

            // Retrieve the price value
            Cell cell5 = row.createCell( startColIndex + 4 );
            cell5.setCellValue( datasource.get( i - 2 ).getFloatField() );
            cell5.setCellStyle( bodyCellStyle );

            // Retrieve the efficiency value
            Cell cell6 = row.createCell( startColIndex + 5 );
            cell6.setCellValue( datasource.get( i - 2 ).getDoubleField() );
            cell6.setCellStyle( bodyCellStyle );
        }
    }

    /*
     * Class clazz = obj.getClass(); PropertyDescriptor pd = new PropertyDescriptor(filed,clazz); Method getMethod =
     * pd.getReadMethod();//获得get方法 Object o = getMethod.invoke(obj);//执行get方法返回一个Object
     */public static int fillReportPaged( Sheet worksheet, int startRowIndex, int startColIndex, List datasource, Class clazz,
                                          String[] propertyNames ) {
        // Row offset
        startRowIndex += 0;

        // Create cell style for the body
        CellStyle bodyCellStyle = worksheet.getWorkbook().createCellStyle();
        bodyCellStyle.setAlignment( CellStyle.ALIGN_CENTER );
        bodyCellStyle.setWrapText( true );
        /*
         * private Long demoId; private int intField; private long longField; private short shortField; private float
         * floatField; private double doubleField;
         */
        // Create body

        int i;
        for ( i = 0; i < datasource.size(); i++ ) {
            // Create a new row
            Row row = worksheet.createRow( startRowIndex + i + 1 );

            for ( int j = 0; j < propertyNames.length; j++ ) {

                Cell cell = row.createCell( startColIndex + j );
                PropertyDescriptor pd;
                try {
                    pd = new PropertyDescriptor( propertyNames[j], clazz );
                    Method getMethod = pd.getReadMethod();// 获得get方法
                    Object o = getMethod.invoke( datasource.get( i ) );

                    cell.setCellValue( o.toString() );

                }
                catch ( Exception e ) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    cell.setCellValue( e.getMessage() );
                }

                cell.setCellStyle( bodyCellStyle );
            }
            /*
             * // Retrieve the id value // Retrieve the brand value Cell cell2 = row.createCell( startColIndex + 1 );
             * cell2.setCellValue( datasource.get( i ).getIntField() ); cell2.setCellStyle( bodyCellStyle ); // Retrieve
             * the model value Cell cell3 = row.createCell( startColIndex + 2 ); cell3.setCellValue( datasource.get( i
             * ).getLongField() ); cell3.setCellStyle( bodyCellStyle ); // Retrieve the maximum power value Cell cell4 =
             * row.createCell( startColIndex + 3 ); cell4.setCellValue( datasource.get( i ).getShortField() );
             * cell4.setCellStyle( bodyCellStyle ); // Retrieve the price value Cell cell5 = row.createCell(
             * startColIndex + 4 ); cell5.setCellValue( datasource.get( i ).getFloatField() ); cell5.setCellStyle(
             * bodyCellStyle ); // Retrieve the efficiency value Cell cell6 = row.createCell( startColIndex + 5 );
             * cell6.setCellValue( datasource.get( i ).getDoubleField() ); cell6.setCellStyle( bodyCellStyle );
             */
        }

        return startRowIndex + i;
    }
}
